package rbac.View.Main.Service.array;

import rbac.View.Main.DoMain.Dto.RoleDto;
import rbac.View.Main.DoMain.Entity.Role;
import rbac.View.Main.DoMain.Vo.RoleVo;
import rbac.View.Main.DoMain.Vo.Result;
import rbac.View.Main.Service.IRoleService;
import rbac.View.Main.Service.IUserService;


public class RoleService extends  BaseService<Role,RoleDto,RoleVo> implements IRoleService {

    private IUserService userservice;

    private static IRoleService singleton;

    private RoleService(){
        data=new Role[DATA_SIZE+1];
        temp=new RoleVo[DATA_SIZE+1];
    }
    public static IRoleService getInstance(){
        if(singleton==null){
            singleton=new RoleService();
        }
        return singleton;
    }

    @Override
    RoleVo convertToVo(Role role) {
        return new RoleVo(role.getId(),role.name,role.dataScope);
    }

    @Override
    Role createEntity(RoleDto dto,int id){
        return new Role(id,dto.name,dto.dataScope);
    }

    @Override
    public  void updateEntity(RoleDto dto,Role role){
        role.name=dto.name;
        role.dataScope=dto.dataScope;
    }

    @Override
    public  Result delete(int id){
        if(existsById(id)){
            return Result.faliure("删除失败，不存在该数据");
        }
        if(userservice==null){
            userservice=UserService.getInstance();
        }
        if(userservice.roleInUse(id)){
            return Result.faliure("删除失败，还有用户具有该角色，不能删除");
        }
        data[id]=null;
        return Result .success("删除成功",null);

    }
}

